home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet 2002 February / Practical Internet February 2002.iso / pc / Software / Browsing / N6setup.exe / NIM.XPI / bin / chrome / aim.jar / content / aim / App.js < prev    next >
Encoding:
Text File  |  2001-10-06  |  35.8 KB  |  1,279 lines

  1.  
  2. var processInviteTimer = -1;
  3.  
  4. // XXX Global to determine if our onload handler has been called yet.
  5. // For some reason, our onunload handler is getting called before onload
  6. // on the first time we create App.xul. This is a workaround.
  7.  
  8. var gFirstTime = 1;
  9.  
  10. function GoAway( target )
  11. {
  12.     var name = target.getAttribute('MsgText');
  13.     if(name=='')
  14.     {
  15.         try 
  16.         {
  17.           name= document.getElementById('tbAway').getElementsByTagName("menuitem")[1].getAttribute('MsgText')
  18.         }
  19.         catch (e)
  20.         {
  21.             dump("goAway(): Some error finding tbAway message!\n Setting default backup message.\n");
  22.             name=aimString("away.DefaultMessage");
  23.         }
  24.     }
  25.  
  26.     locateManager = aimLocateManager();
  27.  
  28.     if ( locateManager )
  29.         locateManager.SetUserInfoAwayMessage(name);
  30. }
  31.  
  32. var targetElement = null;
  33.  
  34. function GoAwayWait(target,wait)
  35. {
  36.     targetElement = target;
  37.     setTimeout('GoAway( targetElement )',wait)
  38. }
  39.  
  40. /*
  41.  * Function: AimAppOnWndLoad()
  42.  *
  43.  * Arguments: None
  44.  * 
  45.  * Return: None
  46.  *
  47.  * Description: Called once app.xul has finished loading. Adds observer 
  48.  *         AimSessionObserver to the AimDataSource. Initializes the
  49.  *        sidebar.
  50. */
  51.  
  52. function AimAppOnWndLoad()
  53. {
  54.   HideMenus();
  55.     
  56. //    dump("AimAppOnWndLoad()\n");
  57.  
  58.     // XXX See note at the top of the file
  59.     gFirstTime = 0;
  60.  
  61.     // Initialize for current online state
  62.     var state = aimRDFDataSource().GetTarget(aimRDFSession(), aimRDFSessionState(), true);
  63.     if (state == null)
  64.         state = aimRDF().GetLiteral("Register");
  65.  
  66.     AimSessionObserver.onAssert(aimRDFDataSource(), aimRDFSession(), aimRDFSessionState(), state);
  67.     aimRDFDataSource().AddObserver(AimSessionObserver);
  68.   
  69.   setTimeout("CheckBuddyPanel()",500);
  70.     
  71.   setAdTimer();
  72. }
  73.  
  74. function HideMenus()
  75. {
  76.     var menu;
  77.     
  78.     if ( aimIsShrimp() == true ) {
  79.         menu=document.getElementById('menu_Help');
  80.         if(menu != null && menu != undefined)
  81.             menu.setAttribute("hidden", "true");
  82.     } else {
  83.         menu=document.getElementById('menu-services');
  84.         if(menu != null && menu != undefined)
  85.             menu.setAttribute("hidden", "true");
  86.         menu=document.getElementById('menu-actions');
  87.         if(menu != null && menu != undefined)
  88.             menu.setAttribute("hidden", "true");
  89.         menu=document.getElementById('menu-ShrimpHelp');
  90.         if(menu != null && menu != undefined)
  91.             menu.setAttribute("hidden", "true");
  92.          menu=document.getElementById('navprintplus');
  93.           if(menu != null && menu != undefined)
  94.               menu.setAttribute("hidden", "true");
  95.     }
  96. }
  97.  
  98. /*
  99.  * Function: SetProcessTimer( interval )
  100.  *
  101.  * Arguments: 
  102.  *      interval -- timeout in milliseconds 
  103.  *
  104.  * Return: None
  105.  *
  106.  * Description: This function sets the global (to App.js) timer 
  107.  * processInviteTimer to fire after the specified interval
  108.  *
  109.  * Author: Syd 4/20/2001
  110.  */
  111.  
  112. function SetProcessTimer( interval )
  113. {
  114.         processInviteTimer = setTimeout('ProcessInviteList()',interval);
  115. }
  116.  
  117. /*
  118.  * Function: ClearProcessTimer()
  119.  *
  120.  * Arguments: None
  121.  *
  122.  * Return: None
  123.  *
  124.  * Description: if the global processInviteTimer != -1, then
  125.  * clear the timer, otherwise return. This function should 
  126.  * be called by the onunload handler or at anytime the state
  127.  * goes from online to offline
  128.  *
  129.  * Author: Syd 4/20/2001
  130.  */
  131.  
  132. function ClearProcessTimer()
  133. {
  134.         if ( processInviteTimer != -1 ) {
  135.                 window.clearTimeout( processInviteTimer );
  136.                 processInviteTimer = -1;
  137.         }
  138. }
  139.  
  140. /*
  141.  * Function: AimAppOnWndUnload()
  142.  *
  143.  * Arguments: None
  144.  * 
  145.  * Return: None
  146.  *
  147.  * Description: Called once app.xul has been unloaded. Removes observer 
  148.  *         AimSessionObserver from the AimDataSource. 
  149. */
  150.  
  151. function AimAppOnWndUnload()
  152. {
  153.     // XXX See note at the top of the file
  154.  
  155.         ClearProcessTimer();
  156.  
  157.     if(gFirstTime == 1)
  158.         return;
  159.  
  160. dump( "**************remove observer!!!\n" );
  161.  
  162.     aimRDFDataSource().RemoveObserver(AimSessionObserver);
  163. }
  164.  
  165. //************  Command Handling ****************
  166.  
  167. /*
  168.  * Function: cmdNewBrowser()
  169.  *
  170.  * Arguments: None
  171.  * 
  172.  * Return: None
  173.  *
  174.  * Description: Function invoked from menu or toolbar to create a new 
  175.  *         browser window. 
  176.  * 
  177.  * Bugs: URL is hardcoded. Needs to open a user-specified preference.
  178. */
  179.  
  180. function cmdNewBrowser()
  181. {
  182.     aimCmdNewBrowser("http://home.netscape.com");
  183. }
  184.  
  185. /*
  186.  * Function: cmdNewEmail()
  187.  *
  188.  * Arguments: None
  189.  * 
  190.  * Return: None
  191.  *
  192.  * Description: Function invoked from menu or toolbar to create a new
  193.  *        messenger window. 
  194. */
  195.  
  196. function cmdNewEmail()
  197. {
  198.     aimCmdNewEmail();
  199. }
  200.  
  201.  
  202.  
  203. function cmdNewChat(invScreenName,invProposal,invScreenNames,toExistingWindow,invMode)
  204. {
  205.   inviteArgsObj = {
  206.     inviteProposalScreenName: invScreenName, 
  207.     inviteProposalObj: invProposal,
  208.     invitedScreenNames: invScreenNames,
  209.     inviteToExistingWindow: toExistingWindow,
  210.     inviteMode: invMode
  211.   }
  212. window.openDialog('chrome://aim/content/chatInviteBuddy.xul','_blank','chrome,all,dialog=no',inviteArgsObj);
  213. }
  214.  
  215.  
  216. /*
  217.  * Function: cmdNewIM()
  218.  *
  219.  * Arguments: None
  220.  * 
  221.  * Return: None
  222.  *
  223.  * Description: Function invoked from menu or toolbar to create a new IM 
  224.  *        window. Loads the window into a pre-connect state, e.g., the 
  225.  *        dialog consists of screen name field and an editor. This
  226.  *        function determines which buddy is selected in the list. 
  227.  *        If a buddy is selected (the first one found) and is online,
  228.  *        the screenname field in the IM window is initialized to that
  229.  *        buddy. Otherwise, the screen name field is set to "".
  230. */
  231.  
  232. function cmdNewIM()
  233. {
  234. //var sidebarframe=top.AIMDocumentPath;
  235.         var sidebarframe=getsidebarframe();
  236.         var tab = sidebarframe.document.getElementById("OnlineOrgTabPanel");
  237.         var tree;
  238.         if ( tab.getAttribute('index') == 0 ) // in the online tab and the user hit delete
  239.                 tree = sidebarframe.document.getElementById("OnlineBuddies");
  240.         else
  241.                 tree = sidebarframe.document.getElementById("ListSetup");
  242.   
  243.     if ( AimOnlineAway() )
  244.         ComeBack();
  245.         
  246.     try
  247.     {
  248.         screenName = tree.selectedItems[0].getAttribute("ScreenName");
  249.     }
  250.     catch (e)
  251.     {
  252.         screenName = null;
  253.     }
  254.         if (!aimBuddyIsOnline(screenName)) {
  255.                 screenName = null;
  256.         }
  257.     
  258.     if (screenName != null)
  259.     {
  260.         var pIAimIM = aimIMObject();
  261.         
  262.         if (pIAimIM)
  263.             var pWindow = pIAimIM.GetExistingIM(screenName);
  264.         if (pWindow)
  265.         {
  266.             pWindow.focus();
  267.             return;
  268.         }
  269.     }
  270.     aimIMInvokeIMForm(screenName, null);
  271. }
  272.  
  273. /*
  274.  * Function: getSelectedBuddiesFromList()
  275.  *
  276.  * Arguments: None
  277.  * 
  278.  * Return: array of strings of buddies
  279.  *     that are currently selected (expands Groups to all buddies in group).
  280.  *
  281.  * Description: Function invoked via cmdNewChatSidebar to poll the
  282.  *        Buddy list to find out which buddies (and expanded groups are selected).
  283.  *        This function will always return an Array, the first element will
  284.  *        be null if there was a problem, or no buddies selected.
  285.  *    
  286. */
  287.  
  288. function getSelectedBuddiesFromList() {
  289.         var sidebarframe=top.AIMDocumentPath;
  290.         var tree = sidebarframe.document.getElementById("OnlineBuddies");
  291.         var items=tree.selectedItems;    //Array of all the selected elements, both groups and buddies
  292.         var selectedBuddies = new Array();
  293.         var l = 0;  //Keep track of the selectedBuddies array.
  294.  
  295.         if (items.length!=0)
  296.         {
  297.                 for (var t=0;t<items.length;t++)
  298.                 {
  299.             /* Make sure the group list is open otherwise we can not
  300.              * iterate through the children. This has the added side
  301.              * effect that groups are opened when you click on them 
  302.              * and hit IM or Chat.
  303.             */
  304.                         items[t].setAttribute("open", true);
  305.                         if (items[t].childNodes.length > 1)    //If this is a group.
  306.                         {
  307.  
  308.                                 var buddies = null;
  309.                                 for (var j = 0; j != items[t].childNodes.length; j++)
  310.                                 {
  311.                                         if (items[t].childNodes[j].nodeName == "treechildren")
  312.                                         {
  313.                                                 buddies = items[t].childNodes[j].childNodes;
  314.                                                 break;
  315.                                         }
  316.                                 }
  317.                                 if (buddies) {
  318.                                         for (var k = 0; k != buddies.length; k++) {
  319.                         //Make sure the buddy isn't already in this list.
  320.                         if (selectedBuddies.toString().search(buddies[k].getAttribute("ScreenName")) == -1)
  321.                         {
  322.                             //Add the buddy to the list.
  323.                                                     selectedBuddies[l++] = buddies[k].getAttribute("ScreenName"); 
  324.                         }
  325.                                         }
  326.                                 }
  327.                         }
  328.                         else //This item is a single buddy.
  329.             {
  330.                 //Make sure the buddy isn't already in this list.
  331.                 if (selectedBuddies.toString().search(items[t].getAttribute("ScreenName")) == -1)
  332.                 {
  333.                     //Add the buddy to the list.
  334.                     selectedBuddies[l++]=items[t].getAttribute("ScreenName");
  335.                 }
  336.                         }
  337.                 }
  338.         }
  339.         return selectedBuddies;
  340. }
  341.  
  342. /*
  343.  * Function: cmdSignOn()
  344.  *
  345.  * Arguments: None
  346.  * 
  347.  * Return: None
  348.  *
  349.  * Description: Called from menu item or toolbar. Passes control to sidebar
  350.  *        panel cmdSignOn() (see SidebarPanel/SidebarPanel.js)
  351. */
  352.  
  353. function cmdSignOn()
  354. {
  355.     //var sidebarframe = top.frames["AppPanel"];
  356.     //var sidebarframe=top.AIMDocumentPath;
  357.     var sidebarframe=getsidebarframe();
  358.  
  359.   if(sidebarframe) {
  360.       sidebarframe.cmdPanelSignOn();
  361.   }
  362.   else
  363.     dump("XXX Error : no sidebarframe\n");
  364. }
  365.  
  366. /*
  367.  * Function: cmdSignOff()
  368.  *
  369.  * Arguments: None
  370.  * 
  371.  * Return: None
  372.  *
  373.  * Description: Calls aimSessionLogoff to close current IM session. 
  374.  *     
  375.  *
  376. */
  377.  
  378. function cmdSignOff()
  379. {
  380.   /* Set pref to be false so that auto login doesnt get fired for every launch */
  381.   aimPrefsManager().SetBoolPref("aim.session.appfirstlogin", false, null,true);
  382.   aimSessionLogoff();
  383. }
  384.  
  385. /*
  386.  * Function: cmdCancelSignOn()
  387.  *
  388.  * Arguments: None
  389.  * 
  390.  * Return: None
  391.  *
  392.  * Description: Called during connection startup if cancel button is pressed.
  393.  *         Cancels the IM session by calling cmdSignOff().
  394.  *        
  395. */
  396.  
  397. function cmdCancelSignOn()
  398. {
  399.     cmdSignOff();
  400. }
  401.  
  402. /*
  403.  * Function: cmdClose()
  404.  *
  405.  * Arguments: None
  406.  * 
  407.  * Return: None
  408.  *
  409.  * Description: Invoked from close menu item, or from cmdSignOffAndClose(),
  410.  *        below. Causes the app window to be closed.
  411.  *
  412. */
  413.  
  414. function cmdClose()
  415. {
  416.    aimCmdClose();
  417. }
  418.  
  419. /*
  420.  * Function: cmdSignOffAndClose()
  421.  *
  422.  * Arguments: None
  423.  * 
  424.  * Return: None
  425.  *
  426.  * Description: Called in response to user selecting signoff and close menu
  427.  *        item. Signs off the current IM session and closes the 
  428.  *        standalone window.
  429. */
  430.  
  431. function createInstance( contractid, iidName )
  432. {
  433.     var iid = eval( "Components.interfaces." + iidName );
  434.         return Components.classes[ contractid ].createInstance( iid );
  435. }
  436.  
  437. function cmdExportBuddyList()
  438. {
  439.         nsIFilePicker = Components.interfaces.nsIFilePicker;
  440.     var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  441.  
  442.     fp.init(window, aimString("title.exportbudlist"), nsIFilePicker.modeSave);
  443.     fp.appendFilter(aimString("budlist.title"), aimString("budlist.filter"));
  444.     fp.appendFilters(nsIFilePicker.filterAll);
  445.  
  446.     try {
  447.         var ret = fp.show();
  448.         if ( ret == nsIFilePicker.returnOK || ret == nsIFilePicker.returnReplace) {
  449.             var status;
  450.             status = aimBuddyExportBuddyList(fp.file);
  451. /*
  452.             if ( status == false )
  453.                 aimErrorBox(aimString("msg.exporttryagain"));
  454.             else
  455.                 aimErrorBox(aimString("msg.exportsuccess"));
  456. */
  457.         }
  458.     } 
  459.     catch (ex) {
  460.         aimErrorBox(aimString("msg.exportfailure"));
  461.     }
  462.     return;
  463. }
  464.  
  465.  
  466. function cmdImportBuddyList()
  467. {
  468.  
  469.     nsIFilePicker = Components.interfaces.nsIFilePicker;
  470.     var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  471.  
  472.     fp.init(window, aimString("title.importbudlist"), nsIFilePicker.modeOpen);
  473.     fp.appendFilter(aimString("budlist.title"), aimString("budlist.filter"));
  474.     fp.appendFilters(nsIFilePicker.filterAll);
  475.  
  476.     try {
  477.         var ret = fp.show();
  478.         if ( ret == nsIFilePicker.returnOK ) {
  479.             var status;
  480.             aimBuddyImportBuddyList(fp.file);
  481.         }
  482.     }
  483.     catch (ex) {
  484.         aimErrorBox(aimString("msg.importfailure"));
  485.     }
  486.     return;
  487. }
  488.  
  489. function cmdSignOffAndClose()
  490. {
  491.     cmdSignOff();
  492.     cmdClose();
  493. }
  494.  
  495. /*
  496.  * Function: cmdAddBuddy()
  497.  *
  498.  * Arguments: None
  499.  * 
  500.  * Return: None
  501.  *
  502.  * Description: Called in response to menu item of toolbar button in standalone
  503.  *        for adding a buddy to a buddy group. Opens BuddyAddBuddy.xul to
  504.  *        place a dialog that can be used to add a buddy.
  505.  *
  506.  * Bugs:    Needs to support inline editing.
  507.  * 
  508. */
  509.  
  510. function cmdAddBuddy()
  511. {
  512.     //var sidebarframe = top.frames["AppPanel"];
  513.     //var sidebarframe = window // in new UI land
  514.     
  515.     
  516.     //dump("AIMDocumentPath: "+top.AIMDocumentPath+"\n");
  517.     //sidebarframe=top.AIMDocumentPath;
  518.     var sidebarframe=getsidebarframe();
  519.  
  520.     var foundOne = false;
  521.     var selectedGroup = "";
  522.  
  523.   var elt = sidebarframe;
  524.   if (!sidebarframe)
  525.     elt = window;
  526.   var tree = elt.document.getElementById("ListSetup");
  527.    if (!tree )
  528.       return;
  529.     var groups = aimBuddyTreeFindGroups( tree );
  530.  
  531.     if ( !groups || groups.length == 0 ) {
  532.         aimErrorBox(aimString("msg.NoGroups"));
  533.         return;
  534.     }
  535.  
  536.      var selectedGroups = new Array();
  537.      var j=0;
  538.  
  539.      for (var i = 0; i != groups.length; i++) {
  540.          if (groups[i].getAttribute("selected") == "true") {
  541.              selectedGroups[j++] = groups[i].getAttribute("Name");
  542.          }
  543.      }
  544.  
  545.     openDialog("chrome://aim/content/BuddyAddBuddy.xul", "", 
  546.         "modal=yes,titlebar,chrome", selectedGroups, tree, null);
  547.         
  548.     
  549.         
  550. }
  551.  
  552. /*
  553.  * Function: cmdAddGroup()
  554.  *
  555.  * Arguments: None
  556.  * 
  557.  * Return: None
  558.  *
  559.  * Description: Called in response to menu item of toolbar button in standalone
  560.  *        for adding a buddy group. Opens BuddyAddGroup.xul to place a 
  561.  *        dialog that can be used to add a buddy group.
  562.  *
  563.  * Bugs:    Needs to support inline editing.
  564.  * 
  565. */
  566.  
  567. function cmdAddGroup()
  568. {
  569.     openDialog("chrome://aim/content/BuddyAddGroup.xul", "", "modal=yes,titlebar,chrome");
  570. }
  571.  
  572. /*
  573.  * Function: SelectedGroup()
  574.  *
  575.  * Arguments: None
  576.  * 
  577.  * Return: None
  578.  *
  579.  * Description: Constructor function for SelectedGroup objects
  580.  *         
  581.  *        
  582. */
  583.  
  584. function SelectedGroup()
  585. {
  586.     var selected = "false";
  587.     var name = null;
  588.     var selectedBuddies = null;
  589. }
  590.  
  591. /*
  592.  * Function: SelectedBuddy()
  593.  *
  594.  * Arguments: None
  595.  * 
  596.  * Return: None
  597.  *
  598.  * Description: Constructor function for SelectedBuddy objects
  599.  *         
  600. */
  601.  
  602. function SelectedBuddy()
  603. {
  604.     var selected = "false"
  605.     var name = null;
  606. }
  607.  
  608. /*
  609.  * Function: cmdDelete()
  610.  *
  611.  * Arguments: None
  612.  * 
  613.  * Return: None
  614.  *
  615.  * Description: Called in response to user clicking delete toolbar button or
  616.  *        selecting delete menu item. Deletes all selected groups (and
  617.  *        the buddies they contain) and all selected buddies.
  618. */
  619.  
  620. function cmdDelete()
  621. {    
  622.     // Delete all selected groups and or buddies
  623.  
  624.     //var sidebarframe = top.frames["AppPanel"];
  625.     //var sidebarframe=window // in new UI land
  626.     //sidebarframe=top.AIMDocumentPath;
  627.       var sidebarframe=getsidebarframe();
  628.  
  629.     var selectedGroup = "";
  630.     var wasSelected = false;
  631.  
  632.     var tab = sidebarframe.document.getElementById("OnlineOrgTabPanel");
  633.     var tree;
  634.     if ( tab.getAttribute('index') == 0 ) // in the online tab and the user hit delete
  635.         tree = sidebarframe.document.getElementById("OnlineBuddies");
  636.     else
  637.         tree = sidebarframe.document.getElementById("ListSetup");
  638.  
  639.     if (!tab)
  640.         return;
  641.     if (!tree )
  642.         return;
  643.  
  644.     var groups = aimBuddyTreeFindGroups( tree );
  645. //    dump( "Groups is " + groups + "\n" );
  646. //    dump("Num groups is " + groups.length + "\n");
  647.  
  648.     top.selectedGroups = new Array();
  649.         
  650.     for (var i = 0; i != groups.length; i++) {
  651.  
  652.         // make a record for this group
  653.  
  654.         top.selectedGroups[i] = new SelectedGroup;
  655.         top.selectedGroups[i].selectedBuddies = new Array();
  656.         top.selectedGroups[i].name = groups[i].getAttribute("Name");
  657.         if (groups[i].getAttribute("selected") == "true") {
  658.             wasSelected = "true";
  659.             top.selectedGroups[i].selected = "true";
  660.         }
  661.         else
  662.             top.selectedGroups[i].selected = "false";
  663.  
  664.         // find the buddy nodes for this group.
  665.  
  666.         var buddies = null;
  667.         for (var j = 0; j != groups[i].childNodes.length; j++)
  668.         {
  669.             if (groups[i].childNodes[j].nodeName == "treechildren")
  670.             {
  671.                 // found it.
  672.  
  673.                 buddies = groups[i].childNodes[j].childNodes;
  674.                 break;
  675.             }
  676.         }
  677.  
  678.         // if there were buddies, collect info on them
  679.  
  680.         if (buddies)
  681.         {
  682.             for (var j = 0; j != buddies.length; j++)
  683.             {
  684.                 // create a record for this buddy
  685.  
  686.                 top.selectedGroups[i].selectedBuddies[j] = new SelectedBuddy;
  687.                 if (buddies[j].getAttribute("selected") == "true") {
  688.                     // buddy is selected. Save the name 
  689.                     // and set the selected flag to true
  690.  
  691.                     wasSelected = "true";
  692.                     top.selectedGroups[i].selectedBuddies[j].name = buddies[j].getAttribute("ScreenName");    
  693.                     top.selectedGroups[i].selectedBuddies[j].selected = "true";
  694.                 }
  695.                 else 
  696.                     top.selectedGroups[i].selectedBuddies[j].selected = "false";
  697.             }
  698.         }
  699.     }
  700.  
  701.     // now we have all we need to know. Delete the selected buddies
  702.     // and the selected groups
  703.  
  704.     for ( var i = 0; i != top.selectedGroups.length; i++ ) {
  705.  
  706.         selectedGroup = top.selectedGroups[i].name;
  707.         
  708.         // first the group
  709.  
  710.         if ( top.selectedGroups[i].selected == "true" ) {
  711.             if ( selectedGroup != null && selectedGroup != "" ) {
  712.                 answer = top.confirm(
  713.                     aimString("confirm.DeleteGroup").replace(/%GroupName%/, selectedGroup) );
  714.                 if ( answer == true )
  715.                     aimBuddyRemoveBuddyGroup(selectedGroup);
  716.             }
  717.         }
  718.         else {
  719.             // we didn't delete the group, so see if any buddies
  720.             // in the group need to be deleted.
  721.  
  722.             for ( var j = 0; j != top.selectedGroups[i].selectedBuddies.length; j++ ) {
  723.                 if ( top.selectedGroups[i].selectedBuddies[j].selected == "true" ) {    
  724.                     selectedBuddy = top.selectedGroups[i].selectedBuddies[j].name;
  725.                     if ( selectedBuddy && selectedBuddy != "" ) {
  726. //dump( "Deleting " + selectedBuddy + " from " + selectedGroup + "\n" );
  727.  
  728.                         answer = top.confirm(
  729.                             aimString("confirm.DeleteBuddy").replace(/%BuddyName%/, selectedBuddy).replace(/%GroupName%/, selectedGroup) );
  730.                         if ( answer == true ) 
  731.                             aimBuddyRemoveBuddy(selectedGroup, selectedBuddy);
  732.                     }                    
  733.                 }
  734.             }
  735.         }
  736.     }
  737.  
  738.     if ( wasSelected == false )
  739.         aimErrorBox( aimString("msg.NothingToDelete") );
  740. }
  741.  
  742. function cmdAbEditCard()
  743. {
  744.     //sidebarframe=top.AIMDocumentPath;
  745.         var sidebarframe=getsidebarframe();
  746.         var selectedGroup = "";
  747.         var wasSelected = false;
  748.  
  749.         var tree = sidebarframe.document.getElementById("ListSetup");
  750.     var tab = sidebarframe.document.getElementById("OnlineOrgTabPanel");   
  751.      if ( tab.getAttribute('index') == 0 ) // in the online tab and the user hit delete
  752.                 return;
  753.  
  754.         var groups = aimBuddyTreeFindGroups( tree );
  755. //        dump( "Groups is " + groups + "\n" );
  756. //        dump("Num groups is " + groups.length + "\n");
  757.  
  758.         top.selectedGroups = new Array();
  759.  
  760.         for (var i = 0; i != groups.length; i++) {
  761.  
  762.                 // make a record for this group
  763.  
  764.                 top.selectedGroups[i] = new SelectedGroup;
  765.                 top.selectedGroups[i].selectedBuddies = new Array();
  766.                 top.selectedGroups[i].name = groups[i].getAttribute("Name");
  767.                 if (groups[i].getAttribute("selected") == "true") {
  768.                         wasSelected = "true";
  769.                         top.selectedGroups[i].selected = "true";
  770.                 }
  771.                 else
  772.                         top.selectedGroups[i].selected = "false";                                                   // find the buddy nodes for this group.
  773.  
  774.                 var buddies = null;
  775.                 for (var j = 0; j != groups[i].childNodes.length; j++)
  776.                 {
  777.                         if (groups[i].childNodes[j].nodeName == "treechildren")
  778.                         {
  779.                                 // found it.
  780.  
  781.                                 buddies = groups[i].childNodes[j].childNodes;
  782.                                 break;
  783.                         }
  784.                 }
  785.  
  786.  
  787.  
  788.                 if (buddies)
  789.                 {
  790.                         for (var j = 0; j != buddies.length; j++)
  791.                         {
  792.  
  793.                                 top.selectedGroups[i].selectedBuddies[j] = new SelectedBuddy;
  794.                                 if (buddies[j].getAttribute("selected") == "true") {
  795.                                         // buddy is selected. Save the name
  796.                                         // and set the selected flag to true
  797.  
  798.                                         wasSelected = "true";
  799.                                         top.selectedGroups[i].selectedBuddies[j].name = buddies[j].getAttribute("ScreenName");
  800.                                         top.selectedGroups[i].selectedBuddies[j].selected = "true";
  801.                                 }
  802.                                 else
  803.                                         top.selectedGroups[i].selectedBuddies[j].selected = "false";
  804.                         }
  805.                 }                     
  806.          }
  807.  
  808.         for ( var i = 0; i != top.selectedGroups.length; i++ ) {
  809.  
  810.                 selectedGroup = top.selectedGroups[i].name;
  811.  
  812.  
  813.                 if ( top.selectedGroups[i].selected == "true" ) {
  814.                      aimErrorBox(aimString("msg.SelectBuddy")); 
  815.         }
  816.                 else {
  817.                         for ( var j = 0; j != top.selectedGroups[i].selectedBuddies.length; j++ ) {
  818.                          if ( top.selectedGroups[i].selectedBuddies[j].selected == "true" ) {
  819.                              selectedBuddy = top.selectedGroups[i].selectedBuddies[j].name;
  820.                             if ( selectedBuddy && selectedBuddy != "" ) {
  821.             var aimABInfo1= aimGetIMManager().QueryInterface(Components.interfaces.nsIAimABInfo);
  822.             var abURI= new Object();
  823.             aimABInfo1.GetABURI(selectedBuddy, abURI);
  824.             var abURI = aimABInfo().GetABURI(selectedBuddy);
  825.             var rdf = aimRDF();
  826.             var card = rdf.GetResource(abURI);
  827.             card = card.QueryInterface(Components.interfaces.nsIAbCard);
  828.         //This is not currently suppoerted by AddressBook - ToDO later//
  829.         //var updateview=DisplayCardViewPane(abURI);
  830.  
  831.         //Workaround - Launch addressbook instead of ab card for selected buddy
  832.         //toOpenWindowByType("mail:adressbook", "chrome://messenger/content/addressbook/addressbook.xul");        
  833.         goEditCardDialog("moz-abmdbdirectory://abook.mab",card,null,abURI);
  834.                            }
  835.                         }                  
  836.                   }
  837.                 }
  838.                 }
  839.  
  840. }       
  841.  
  842.  
  843. function cmdCustom()
  844. {
  845.   url = aimString("app.custom.url");
  846.   aimCmdNewBrowser(url);
  847. }
  848.  
  849. function cmdAdClick()
  850. {
  851.   //var adLink = aimString("app.ad.link");
  852.       var adLink;
  853.    if (aimIsShrimp())
  854.        adLink = aimRegionString("shrimp.ad.link")
  855.    else
  856.        adLink = aimRegionString("app.ad.link");
  857.  
  858.  
  859.   var adMini = aimPrefsManager().GetBoolPref("aim.ad.minibrowser", null, true);
  860.  
  861.   // adMini will be true only for shrimp
  862.   if (adMini) {
  863.     var tearWin = window.open(adLink,adLink,'personalbar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,menubar=no');
  864.     if (window.focus)
  865.         tearWin.focus();
  866.   }
  867.   else    
  868.       aimCmdNewBrowser(adLink);
  869. }
  870.  
  871. var ad_access=1;
  872.  
  873. function setAdTimer()
  874. {
  875.  
  876.   var adButton = top.document.getElementById("adButton");
  877.  
  878.   //var adImage = aimRegionString("app.ad.image")+"?access=" + ad_access;
  879.  
  880.     var adImage;
  881.     if (aimIsShrimp())
  882.         adImage = aimRegionString("shrimp.ad.image")+"?access=" + ad_access
  883.     else
  884.         adImage = aimRegionString("app.ad.image")+"?access=" + ad_access;
  885.   
  886.   ad_access++;
  887.   var adTimer = aimString("app.ad.timer");
  888.  
  889.   if(adButton) {
  890.         adButton.setAttribute("src", adImage);
  891.   }
  892.  
  893.   if(window)
  894.     window.setTimeout('setAdTimer()', adTimer);
  895. }
  896.  
  897. function aboutShrimp(url)
  898. {
  899.     var tearWin = window.open(url,url,'personalbar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,menubar=no');
  900.     if (window.focus)
  901.         tearWin.focus();
  902. }
  903.  
  904. function getSidebarDs()
  905. {
  906. var PANELS_RDF_FILE = "UPnls";
  907. try {
  908.      var locator_service = Components.classes["@mozilla.org/file/directory_service;1"].getService();
  909.       if (locator_service)
  910.         locator_service = locator_service.QueryInterface(Components.interfaces.nsIProperties);
  911.       var sidebar_file = locator_service.get(PANELS_RDF_FILE, Components.interfaces.nsIFile);
  912.       if (!sidebar_file.exists()) {
  913.         //Sidebar panels file does not exist?
  914.         return;
  915.       }
  916.         var file_url = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIFileURL);
  917.         file_url.file = sidebar_file;
  918.         return file_url.spec;
  919.     } catch (ex) {
  920.        throw(ex);
  921.     }
  922.  }
  923.  
  924. function CheckBuddyPanel()
  925. {
  926.     
  927.     var RDF = aimRDF();
  928.     var panel_list =RDF.GetDataSource(getSidebarDs()).GetTarget(RDF.GetResource("urn:sidebar:current-panel-list"),RDF.GetResource("http://home.netscape.com/NC-rdf#"+"panel-list"),  true);
  929.     if (panel_list) {
  930.         panel_list.QueryInterface(Components.interfaces.nsIRDFResource);
  931.     }
  932.     var container = Components.classes["@mozilla.org/rdf/container;1"].createInstance();
  933.     container = container.QueryInterface(Components.interfaces.nsIRDFContainer);
  934.     try { 
  935.        container.Init(RDF.GetDataSource(getSidebarDs()), panel_list);
  936.        } catch (ex)
  937.      {
  938.       throw(ex);
  939.      }
  940.     var panel_resource = RDF.GetResource("urn:sidebar:panel:im-panel");
  941.     var im_resource=RDF.GetResource("urn:sidebar:3rdparty-panel:chrome://aim/content/SidebarPanel.xul");
  942.     var panel_index = container.IndexOf(panel_resource);
  943.     var panel_index2 = container.IndexOf(im_resource);
  944.     if ((panel_index == -1) && (panel_index2==-1))
  945.     {
  946.       if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) 
  947.       { 
  948.          window.sidebar.addPanel ("Buddy List","chrome://aim/content/SidebarPanel.xul",""); 
  949.       }
  950.     } 
  951. }
  952.  
  953.  
  954.  /******
  955.   * Request info about a user and display it in a tooltip (after callback).
  956.   ******/
  957.  
  958.  function fillInfoTooltip(aTreecell)
  959.  {
  960.    // this check is required to prevent redundant 
  961.    // re-requests while waiting for the callback
  962.    if (!aTreecell._RequestingInfo) {
  963.      var name = getScreenNameFromNode(aTreecell);
  964.      var tooltip = document.getElementById("infoTooltip");
  965.  
  966.      if (name && name != "" && tooltip) {
  967.        // if this is a mac do this bad hack to hide the tooltip
  968.        if (navigator.appVersion.indexOf("Macintosh") != -1)
  969.          document.getElementById("hideload").setAttribute("collapsed", "true");
  970.        else
  971.          tooltip.setAttribute("collapsed", "true");
  972.        aTreecell._RequestingInfo = true;
  973.  
  974.        // send the request and wait for callback
  975.        var listenerCap = new TooltipCapListener(aTreecell, tooltip);
  976.        try {
  977.         aimLocateManager().RequestUserInfoCapabilities(listenerCap, name);
  978.        } catch (ex) {
  979.          dump("* ERROR: unable to retrieve AIM user info for '"+name+"'\n* REASON: " + ex);
  980.        }
  981.      }
  982.    }
  983.  }
  984.  
  985.  /******
  986.   * Search up the tree for first treeitem node.
  987.   ******/
  988.  
  989.  function getScreenNameFromNode(aNode)
  990.  {
  991.    var node = aNode;
  992.    while (node.localName != "treeitem") {
  993.      node = node.parentNode;
  994.    }
  995.    return node ? node.getAttribute("ScreenName") : null;
  996.  }
  997.  
  998.  /******
  999.   * Returns a string with the difference between two dates looking like: "x days, x hours, x minutes."
  1000.   * Both parameters are dates in seconds since 1/1/1970 00:00:00.
  1001.   ******/
  1002.  
  1003.  function getPrettyDateDiff(aDate2, aDate1)
  1004.  {
  1005.    var diff = aDate2-aDate1;
  1006.    var pretty = "";
  1007.    
  1008.    var dayDiff = Math.floor(diff / 86400);
  1009.    diff %= 86400;
  1010.    if (dayDiff > 0) {
  1011.      pretty += dayDiff + " day" + (dayDiff == 1 ? "" : "s");
  1012.    }
  1013.    
  1014.    var hourDiff = Math.floor(diff / 3600);
  1015.    diff %= 3600;
  1016.    if (hourDiff > 0) {
  1017.      pretty += (pretty.length > 0 ? ", " : "") + hourDiff + " " + aimString("tooltip.hour") + (hourDiff == 1 ? "" : "s");
  1018.    }
  1019.    
  1020.    var minDiff = Math.floor(diff / 60);
  1021.    if (minDiff > 0) {
  1022.      pretty += (pretty.length > 0 ? ", " : "") + minDiff + " " + aimString("tooltip.minute") + (minDiff == 1 ? "" : "s");
  1023.    }
  1024.    
  1025.    return pretty;
  1026.  }
  1027.  
  1028.  
  1029.  /******
  1030.   * Event listener used for RequestUserInfoCapabilities
  1031.   ******/
  1032.  
  1033.  function TooltipCapListener(aTreecell, aTooltip)
  1034.  {
  1035.    this.mTreecell = aTreecell;
  1036.    this.mTooltip = aTooltip;
  1037.  }
  1038.  
  1039.  TooltipCapListener.prototype = {
  1040.     OnRequestUserInfoCapabilitiesComplete: function(aScreenName, aUserObj)
  1041.      {
  1042.      var now = new Date().getTime()/1000;
  1043.      // TODO: search within mTooltip for text elements instead of id search
  1044.      
  1045.      // fill the screen name
  1046.      var elName = document.getElementById("infoTooltipName");
  1047.      var displayname;
  1048.      var nickname;
  1049.      aimABInfo = aimGetIMManager().QueryInterface(Components.interfaces.nsIAimABInfo);
  1050.      if (aimABInfo != null) {
  1051.           try {
  1052.                nickname = aimABInfo.GetNickname(aScreenName);
  1053.           } catch (e) {}
  1054.           if (nickname == null || nickname == "" || nickname == undefined) {
  1055.       // if the code was caught, displayname will be undefined
  1056.                try {
  1057.                     displayname = aimABInfo.GetDisplayName(aScreenName);
  1058.                } catch (e) {}
  1059.            // if the code was caught, displayname will be undefined
  1060.                if (displayname == null || displayname == "" || displayname == undefined)
  1061.                     elName.setAttribute("value", aScreenName);
  1062.                else
  1063.                     elName.setAttribute("value", displayname);
  1064.           }
  1065.           else
  1066.                elName.setAttribute("value", nickname);
  1067.      }
  1068.      else
  1069.           elName.setAttribute("value", nickname);
  1070.  
  1071.      dump ("aScreenName is: " + aScreenName + "\n");
  1072.      dump ("nickname is: " + nickname + "\n");
  1073.      dump ("displayname is: " + displayname + "\n");
  1074.      dump ("elName.value is: " + elName.getAttribute("value") + "\n");
  1075.  
  1076.      
  1077.      // fill the warnings line
  1078.      var warnings = document.getElementById("infoTooltipWarnings");
  1079.      warnings.setAttribute("value", aUserObj.GetWarningPercent() + "%");
  1080.      var warningsId = document.getElementById("infoTooltipWarningsId");
  1081.      warningsId.setAttribute("value", aimString("tooltip.warnings") + ":");
  1082.  
  1083.      // fill the online line
  1084.      var elOnline = document.getElementById("infoTooltipOnlineTime");
  1085.      var since = aUserObj.GetOnlineSinceTime();
  1086.      elOnline.setAttribute("value", getPrettyDateDiff(now, since));
  1087.      var elOnlineId = document.getElementById("infoTooltipOnlineTimeId");
  1088.      elOnlineId.setAttribute("value", aimString("tooltip.online") + ":");
  1089.      
  1090.      // fill the status line
  1091.      var elStatus = document.getElementById("infoTooltipStatus");
  1092.      var elStatusTime = document.getElementById("infoTooltipStatusTime");
  1093.      if (aUserObj.IsIdle()) {
  1094.        elStatus.setAttribute("value", aimString("tooltip.idle") + ":");
  1095.        var statusDate = aUserObj.GetIdleSinceTime();
  1096.        elStatusTime.setAttribute("value", getPrettyDateDiff(now, statusDate));
  1097.      } else if (aUserObj.IsAway()) {
  1098.        elStatus.setAttribute("value", aimString("tooltip.status") + ":");
  1099.        elStatusTime.setAttribute("value", aimString("tooltip.away"));
  1100.      } else {
  1101.        elStatus.setAttribute("value", aimString("tooltip.status") + ": ");
  1102.        elStatusTime.setAttribute("value", aimString("tooltip.active"));
  1103.      }
  1104.  
  1105.   // fill in the Service Type (AOL, Internet, Administrator...)     
  1106.      var elFlagService = document.getElementById("infoTooltipFlagValue");
  1107.      var flagServiceValue = aUserObj.GetFlags();
  1108.      var serviceString = "";
  1109.      var serviceEnums = Components.interfaces.nsAimServiceTypes;
  1110.      if (flagServiceValue & serviceEnums.Transient)
  1111.         serviceString = aimString("IdsflagTrial.label") + " ";
  1112.      if (flagServiceValue & serviceEnums.Aol)
  1113.         serviceString = serviceString + aimString("IdsflagAol.label") + " ";
  1114.      if (flagServiceValue & serviceEnums.Free)
  1115.         serviceString = serviceString + aimString("IdsflagInternet.label") + " ";
  1116.      if (flagServiceValue & serviceEnums.Administrator)
  1117.         serviceString = serviceString + aimString("IdsflagAdministrator.label") + " ";
  1118.      
  1119.    elFlagService.setAttribute("value", serviceString);
  1120.      var elFlagServiceId = document.getElementById("infoTooltipFlag");
  1121.      elFlagServiceId.setAttribute("value", aimString("tooltip.service") + ":");
  1122.  
  1123.   // fill in the Capabilities
  1124.      var elCapabilities = document.getElementById("capabilitiesValue");
  1125.      try {
  1126.         var capMask = aUserObj.GetCapabilities();
  1127.         var capEnums = Components.interfaces.nsAimCapabilitesMask;
  1128.         var capString = "";
  1129.         if (capEnums.chat & capMask)
  1130.             capString += aimString("tooltip.chat");
  1131.         elCapabilities.setAttribute("value", capString);
  1132.      } catch (e) {}
  1133.      var elCapabilitiesId = document.getElementById("capabilities");
  1134.      elCapabilitiesId.setAttribute("value", aimString("tooltip.capabilities") + ":");
  1135.  
  1136.      // show the tooltip
  1137.      // if this is a mac do this bad hack to hide the tooltip
  1138.      if (navigator.appVersion.indexOf("Macintosh") != -1)
  1139.          document.getElementById("hideload").removeAttribute("collapsed");
  1140.      else
  1141.          this.mTooltip.removeAttribute("collapsed");
  1142.      this.mTreecell._RequestingInfo = false;
  1143.      },
  1144.  
  1145.     OnRequestUserInfoCapabilitiesError: function(aScreenName, aError)
  1146.      {
  1147.      dump("Error while loading user capabilities information: " + aError);
  1148.      this.mTreecell._RequestingInfo = false;
  1149.      }
  1150.  };
  1151.  
  1152.  
  1153. function cmdChangePassword() {
  1154.     window.openDialog('chrome://aim/content/AimChangePassword.xul','','modal=no,titlebar,chrome,centerscreen',null);
  1155. }
  1156.  
  1157. function cmdConfirmAccount() {
  1158.     window.openDialog('chrome://aim/content/AimConfirmAccount.xul','','modal=yes,titlebar,chrome,centerscreen',null);
  1159. }
  1160.  
  1161.  
  1162. /*
  1163. * Name: CommandUpdate_App
  1164. *
  1165. * Arguments: none. 
  1166. *
  1167. * Description:
  1168. *
  1169. * This function is called by the command update handler. Based on the events you specify this get called.
  1170. * Right now it is used only by delete.
  1171. * Return Value: none
  1172. *
  1173. * Author: Suresh Kasinathan<suresh@netscape.com> 8/29/01
  1174. *
  1175. */
  1176.  
  1177. function CommandUpdate_App()
  1178. {
  1179.   goUpdateCommand("cmd_delete");
  1180. }
  1181.  
  1182.  
  1183. var AppController =
  1184. {
  1185.   
  1186.   supportsCommand: function(command)
  1187.   {
  1188.     switch (command) {
  1189.       case "cmd_delete":
  1190.         return true;
  1191.         break;
  1192.       default:
  1193.         return true;
  1194.     }
  1195.   },
  1196.  
  1197.   isCommandEnabled: function(command)
  1198.   {
  1199.     switch (command) {
  1200.       case "cmd_delete":
  1201.         return true;
  1202.         break;
  1203.       default:
  1204.         return true;
  1205.     }
  1206.   },
  1207.  
  1208.   doCommand: function(command)
  1209.   {
  1210.     switch (command) {
  1211.       case "cmd_delete":
  1212.         cmdDelete();
  1213.         break;
  1214.     }
  1215.   },
  1216.  
  1217.   onEvent: function(event)
  1218.   {
  1219.     dump("Nothing for now ...\n");
  1220.   }
  1221. };
  1222.  
  1223. /*
  1224. * Name: SetupCommandUpdateHandlers
  1225. *
  1226. * Arguments: none. 
  1227. *
  1228. * Description:
  1229. *
  1230. * This function initializes command update handler. This is called by AppSessionObserver.
  1231. *
  1232. * Return Value: none
  1233. *
  1234. * Author: Suresh Kasinathan<suresh@netscape.com> 8/29/01
  1235. *
  1236. */
  1237.  
  1238. function AimSetupCommandUpdateHandlers()
  1239. {
  1240.   var sidebarframe=getsidebarframe();
  1241.   var onlineTree;
  1242.   var listsetupTree;
  1243.   if (sidebarframe) {
  1244.     onlineTree = sidebarframe.document.getElementById("OnlineBuddies");
  1245.     listsetupTree = sidebarframe.document.getElementById("ListSetup");
  1246.   }
  1247.   else {
  1248.     onlineTree = document.getElementById("OnlineBuddies");
  1249.     listsetupTree = document.getElementById("ListSetup");
  1250.   }
  1251.   
  1252.   if (onlineTree) {
  1253.     onlineTree.controllers.appendController(AppController);
  1254.   }
  1255.  
  1256.   if (listsetupTree) {
  1257.     listsetupTree.controllers.appendController(AppController);
  1258.   }
  1259. }
  1260.  
  1261. /*
  1262. * Name: onSelectionChange
  1263. *
  1264. * Arguments: none. 
  1265. *
  1266. * Description:
  1267. *
  1268. * This function is called whenever the selection changes in Online tree and list setup tree. 
  1269. * Return Value: none
  1270. *
  1271. * Author: Suresh Kasinathan<suresh@netscape.com> 8/29/01
  1272. *
  1273. */
  1274.  
  1275. function onSelectionChange() {
  1276.   goUpdateCommand("cmd_delete");
  1277. }
  1278.